package com.hzhc.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.*;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.enumBean.WorkCarEnum;
import com.hzhc.mapper.TrainPlanZcdetailMapper;
import com.hzhc.service.TrainPlanZcdetailService;
import com.hzhc.utils.EasypoiExcelUtils;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.StringUtils;
import com.hzhc.vo.TrainPlanDetailVo;
import com.hzhc.vo.TrainPlanVo;
import com.hzhc.vo.TrainPlanZcdetailVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 * 装车计划预导入集装箱 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2025-03-21
 */
@Service
public class TrainPlanZcdetailServiceImpl extends ServiceImpl<TrainPlanZcdetailMapper, TrainPlanZcdetail> implements TrainPlanZcdetailService {

    @Override
    public List<TrainPlanZcdetailVo> getDetailList(Page<TrainPlanZcdetailVo> page, TrainPlanZcdetailVo vo) {
        IPage<TrainPlanZcdetailVo> iPage = baseMapper.getDetailList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<List<TrainPlanZcdetailVo>> cntrUploadCheck(MultipartFile file) {
        List<TrainPlanZcdetailVo> list = new ArrayList<>();
        List<String> cntrList = new ArrayList<String>();
        ResultUtil<List<TrainPlanZcdetailVo>> build = new ResultUtil<>();
        if (file != null) {
            try {
                List<TrainPlanZcdetailVo> planCntrVos = EasypoiExcelUtils.importExcel(file, 0, 1, TrainPlanZcdetailVo.class);
                List<TrainPlanZcdetailVo> newPlanCntrVos = new ArrayList<>();
                //-------------------------装车计划数据-------------------------
                LambdaQueryWrapper<TrainPlanZcdetail> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(TrainPlanZcdetail::getDisable,0);
                List<TrainPlanZcdetail> zcData = baseMapper.selectList(wrapper);

                for (TrainPlanZcdetailVo plan : planCntrVos) {
                    String[] cntrNos = plan.getCntrNo().split("/");
                    for (int i = 0; i < cntrNos.length; i++) {
                        TrainPlanZcdetailVo newPlan = new TrainPlanZcdetailVo();
                        BeanUtils.copyProperties(plan,newPlan);
                        newPlan.setCntrNo(cntrNos[i].trim());
                        newPlanCntrVos.add(newPlan);
                    }

                }
                List<String> collect =new ArrayList<>();
                for (TrainPlanZcdetailVo c:newPlanCntrVos) { //校验数据
                    if (ObjectUtil.isEmpty(c)|| StrUtil.isBlank(c.getCntrNo())){
                        c.setErrorInfo("文件中有空数据;");
                        list.add(c);
                        break;
                    }
                    c.setCntrNo(StrUtil.isNotBlank(c.getCntrNo()) ? c.getCntrNo().replace(" ", "").trim():"");
                    String errorInfo = checkCntr(cntrList, c.getCntrNo());
                    if(zcData.size()>0){
                        if(zcData.stream().map(TrainPlanZcdetail::getCntrNo).collect(Collectors.toList()).contains(c.getCntrNo())){
                            errorInfo += "该箱号已存在;";
                        }
                    }

                    c.setErrorInfo(errorInfo);
                    list.add(c);
                }
                build = ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
            }catch (Exception e){
                e.printStackTrace();
                return ResultUtil.build(400,e.getMessage());
            }
        }
        return build;
    }

    @Override
    public ResultUtil<T> importZcTrainPlanCntr(HttpServletRequest request, List<TrainPlanZcdetail> list) {
        boolean b = saveBatch(list);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updatePoolCntr(TrainPlanZcdetail detail) {
        List<TrainPlanZcdetail> trainPlanZcdetails = baseMapper.selectList(new QueryWrapper<TrainPlanZcdetail>().eq("disable", 0)
                .eq("confirm_flag", 0).eq("cntr_no",detail.getCntrNo()).ne("id", detail.getId()));
        if(trainPlanZcdetails.size()>0){
            return ResultUtil.build(400,"该箱号已存在");
        }
        boolean b = updateById(detail);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> deleteZcCntr(Long id) {
        TrainPlanZcdetail trainPlanZcdetail = baseMapper.selectById(id);
        trainPlanZcdetail.setDisable(1);
        boolean b = updateById(trainPlanZcdetail);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    private String checkCntr(List<String> cntrList, String cntrNo) throws Exception {
//        boolean checkDigit = CheckUtils.checkDigit(cntrNo);
//        if (!checkDigit) {
//            return "箱号格式不正确;";
//        }
        if(!Pattern.compile("[A-Z]*").matcher(cntrNo.substring(0,4)).matches()){
            return "箱号前4位必须是大写字母;";
        }
        //箱号校验暂时仅校验位数
        if (cntrNo.trim().length() != 11) {
            return "箱号格式不正确;";
        }
        if (cntrList.contains(cntrNo)) {
            return "箱号重复;";
        }
        cntrList.add(cntrNo);
        return "";
    }
}
